package com.example.project.Service.Impl;

import com.example.project.Service.PictureService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class PictureServiceImpl implements PictureService {
    @Override
    public String upLoadPicture(MultipartFile files) {
        String path = System.getProperty("user.dir");
        File picturePath = new File(path+"/src//main/resources/static/pictures");
        if(!picturePath.exists()){
            picturePath.mkdirs();
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String pictureName = sdf.format(new Date())+"_"+files.getOriginalFilename();
        String t = pictureName.substring(pictureName.lastIndexOf(".") + 1, pictureName.length()).toLowerCase();
        if(!t.equals("jpg")&&!t.equals("png")){
            return "-2";
        }
        File pictureResultPath = new File(picturePath+"/"+pictureName);
        try {
            files.transferTo(pictureResultPath);
            String s = pictureResultPath.getCanonicalPath();
            return s;
        } catch (IOException e) {
            return "-1";
        }
    }
}
